<?php
/*------------------------------------------------------------------------------ 
    
    Create Date :2010-08-13
    Author    : Phan Van Dam
    Copyright(c) 2010 A.D.A Solution. All rights reserved
    
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#            Date          Author       Note
    3.0     thiáº¿u comment  2011/03/10     HoangPhuc     Review code,update comment for source
------------------------------------------------------------------------------*/

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('frmproduct_loadGrid');      
$objXajax->registerFunction('frmproduct_save');
$objXajax->registerFunction('frmproduct_edit');
$objXajax->registerFunction('frmproduct_delete');
$objXajax->registerFunction('frmproduct_fillMenuParent');
$objXajax->registerFunction('frmproduct_init');

function frmproduct_init()
{
    global $objDbSelect,$user_id,$tbl_postfix,$lang;
    $objResponse =  new xajaxResponse();
    $name = 'name_vi';
    $strCbb=createCombobox('cus_unit',array('id',$name),array('name'=>$name,'value'=>'id'),'frmproduct_cbbunit',false) ; 
     $objResponse->addAssign('frmproduct_tdUnit', 'innerHTML',$strCbb);
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}

function frmproduct_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse =  new xajaxResponse();  
    $name_vi = $params['txtnamemenuVi'];
    $name_en = $params['txtnamemenuEn'];
    $parent_id = $params['cbbMenuParent'];
    $unit = $params['cbbunit'];
    $chkquanlity = $params['chkquanlity'];
    $chkdefaultcreate = $params['chkdefaultcreate'];
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql ="UPDATE cus_product_default SET name_vi = '$name_vi',name_en = '$name_en',parent_id = '$parent_id',
            unit = '$unit',checkquanlity='$chkquanlity',date_update = now(),flg_default_create = $chkdefaultcreate  WHERE id =$id"; 
        $objDbSelect->Execute($sql);
        
    }else{
          $sql= " INSERT INTO cus_product_default(name_vi,name_en,active,unit,parent_id,
          date_update,checkquanlity,flg_default_create,product_type) VALUES('$name_vi','$name_en',1,
          '$unit','$parent_id',now(),'$chkquanlity','$chkdefaultcreate',1)"; 
          //echo $sql; die();
          $objDbSelect->Execute($sql); 
     }    
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_frmproduct_loadGrid();');     
    return $objResponse->getXML();
}

function frmproduct_fillMenuParent()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $objResponse =  new xajaxResponse();
    
    $sql = " SELECT id, name_vi as name FROM cus_product_default WHERE 
       product_type = 0 ";   
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $temp ='';
    for($i=0;$i<$count;$i++){
        $id = $arr[$i]['id'];
        $name = $arr[$i]['name'];
        $temp.= "<option value='$id'> $name </option>"  ;
    }
    $temp =  '<select id="frmproduct_cbbMenuParent" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
    $objResponse->addAssign('frmproduct_tdmenuparent', 'innerHTML',$temp);
    return $objResponse->getXML();
}
function frmproduct_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = frmproduct_getGrid(); 
    //echo '<pre>'; print_r($arrGrid); die();   
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/main_product_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmproduct_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmproduct_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function frmproduct_getGrid()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $sqlSelect = " select pt1.id,pt1.active, pt1.name_vi,pt1.name_en,pt2.name_vi as parent_name,pt1.flg_default_create,
                   pt1.unit as unit , pt1.checkquanlity ,cu.name_vi as unitname from cus_product_default as pt1 INNER JOIN  cus_product_default as pt2 ON 
                   pt1.parent_id = pt2.id left join cus_unit cu ON pt1.unit = cu.id  WHERE  pt1.product_type =1  order by pt1.parent_id,pt1.name_vi ";                        
    return $objDbSelect->GetArray($sqlSelect);
}  

    function frmproduct_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM cus_product_default WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi        = $arr[0]['name_vi']; 
        $name_en        = $arr[0]['name_en']; 
        $parent_id     = $arr[0]['parent_id']; 
        $checkquanlity         = $arr[0]['checkquanlity'];
        $flg_default_create         = $arr[0]['flg_default_create'];
        $unit         = $arr[0]['unit'];
               
        
        $objResponse->addScript("$('#frmproduct_txtnamemenuVi').val('$name_vi');");
        $objResponse->addScript("$('#frmproduct_txtnamemenuEn').val('$name_en');");
        $objResponse->addScript("$('#frmproduct_cbbMenuParent').val('$parent_id');");
        $objResponse->addScript("$('#frmproduct_txtprice').val('$price');");
        
        if($checkquanlity==1){
           $objResponse->addScript("$('#frmproduct_chkquanlity').attr('checked','checked');"); 
        }else{
           $objResponse->addScript("$('#frmproduct_chkquanlity').attr('checked',false);");   
        }        
        
        if($flg_default_create==1){
           $objResponse->addScript("$('#frmproduct_chkdefaultcreate').attr('checked','checked');"); 
        }else{
           $objResponse->addScript("$('#frmproduct_chkdefaultcreate').attr('checked',false);");   
        }
        
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
?>
